package com.android.music.prolyric;



import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;

import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class ChangeBackground extends Activity implements ViewFactory{
	
	public static final Integer[] imagesId = {
		R.drawable.girl,		
		R.drawable.bgr1,
		R.drawable.bgr2,
		R.drawable.bgr2,
		R.drawable.bgr3,
		R.drawable.bgr5,
		R.drawable.bgr7
		
	};
	
	public static final Integer[] imagesSample = {
		R.drawable.sample_girl,		
		R.drawable.sample_bgr1,
		R.drawable.sample_bgr2,
		R.drawable.sample_bgr22,
		R.drawable.bgr3,
		R.drawable.bgr5,
		R.drawable.bgr7	
	};
	
	public static final  int[] textColors = {
		R.color.color_girl,
		R.color.color_bgr1,
		R.color.color_bgr2,
		R.color.color_bgr22,
		R.color.color_bgr2,
		R.color.color_bgr22,
		R.color.color_bgr2,
		R.color.color_bgr22
	};
	
	private int currentPosition = -1;
	private ImageSwitcher imageSwitcher;
	//private TextView tvTextDemo ;
	/*private String demoText = 
			"If I had to live my life without you near me "+
			"The days would all be empty "+
			"The nights would seem so long "+
			"With you I see forever oh so clearly "+
			"I might have been in love before "+
			"But it never felt this strong "+
			"Our dreams are young and we both know "+
			"They'll take us where we want to go "+
			"Hold me now "+
			"Touch me now "+
			"I don't want to live without you ";*/

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_background);
        
        
        //tvTextDemo = (TextView)findViewById(R.id.tv_change_bg_text_demo);
        //tvTextDemo.setText("");
        imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        imageSwitcher.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View v) 
			{
				Intent data = new Intent();
				data.putExtra("positionBackground", currentPosition);
				data.setData(Uri.parse("result"));
				setResult(RESULT_OK, data);
				finish();
			}
		});
        @SuppressWarnings("deprecation")
		Gallery hsv = (Gallery) findViewById(R.id.galery);
        hsv.setAdapter(new ImageAdapter(this));
        hsv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageSwitcher.setImageResource(imagesSample[arg2]);
				currentPosition = arg2;
				
				//tvTextDemo.setText(demoText);
				//tvTextDemo.setTextColor(getResources().getColor(textColors[currentPosition]));
			}
		});       
        
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // getMenuInflater().inflate(R.menu.activity_change_background, menu);
        return true;
    }


	public View makeView() {
		ImageView imageView = new ImageView(this);
		imageView.setBackgroundColor(0xFF000000);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new
		ImageSwitcher.LayoutParams(
		LayoutParams.MATCH_PARENT,
		LayoutParams.MATCH_PARENT));
		return imageView;
	}
	
	
	public class ImageAdapter extends BaseAdapter
	{
		
		private Context context;
		private int itemBackground;
		
		
		public ImageAdapter(Context c)
		{
			context = c;
			//---setting the style---
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			itemBackground = a.getResourceId(
			R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}
		
		public int getCount() {
			// TODO Auto-generated method stub
			return imagesSample.length;
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return imagesSample[arg0];
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(imagesSample[arg0]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
			imageView.setBackgroundResource(itemBackground);
			return imageView;
		}
	}
}
